package com.nervenets.general.config;


import com.nervenets.general.enumeration.Platform;
import com.nervenets.general.utils.StringUtils;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.Serializable;

@Configuration
@ConfigurationProperties(prefix = "application", ignoreInvalidFields = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class ApplicationProperties {
    /**
     * 别名
     */
    private String alias;
    /**
     * 域名
     */
    private String baseDomain;
    /**
     * 静态文件域名
     */
    private String baseStaticDomain;
    /**
     * 错误通知邮箱
     */
    private String[] notifyEmails;
    /**
     * 拦截白名单
     */
    private String[] authorizePermitAll = new String[]{};
    /**
     * 验证链接配置
     */
    @Builder.Default
    private SecretUrl secretUrl = new SecretUrl();
    /**
     * Cors配置
     */
    @Builder.Default
    private CorsConfig cors = new CorsConfig();
    /**
     * 静态文件夹路径
     */
    private String staticFileDirectory;
    /**
     * 是否使用静态文件域名
     */
    private boolean useBaseStaticDomain = false;
    /**
     * 是否单点登录
     */
    private boolean sessionSingleSign = true;
    /**
     * 会话过期时间，默认10分钟
     */
    private int sessionTimeout = 1800;
    /**
     * 会话自动续期
     */
    private boolean sessionAutoRenewal = true;
    /**
     * 是否打开功能操作权限
     */
    private boolean openFunctionRole = true;
    /**
     * 限制文件上传的后缀类型
     */
    private String[] defaultAllowedExtensions = new String[]{};
    /**
     * 扫描权限注解的包路径
     */
    private String[] roleScanPackages = new String[]{};
    /**
     * 返回response是否加密
     */
    private boolean responseEncrypt = false;
    /**
     * 请求request是否全局加密
     */
    private boolean requestEncrypt = false;
    /**
     * 请求Token是否加密
     */
    private boolean tokenEncrypt = false;
    /**
     * 自动打开Swagger文档
     */
    private boolean autoOpenSwagger = false;
    /**
     * Token是否设备
     */
    private boolean tokenValidateDevice = true;
    /**
     * Token是否验证IP
     */
    private boolean tokenValidateIp = true;
    /**
     * 需要鉴权的终端
     */
    private Platform[] authorizePlatforms = new Platform[]{Platform.management};
    /**
     * druid 桌面账号配置
     */
    private DruidStatView druid;
    /**
     * 非法链接屏蔽内容
     */
    @Builder.Default
    private String[] invalidUrlContents = new String[]{};

    /**
     * 获取文件的绝对路径
     *
     * @param fileNamePath 文件相对路径
     * @return 文件的绝对路径
     */
    public String getStaticFileDirectory(String fileNamePath) {
        if (StringUtils.isBlank(fileNamePath)) return fileNamePath;
        if (fileNamePath.startsWith(File.separator)) return staticFileDirectory + fileNamePath;
        return staticFileDirectory + File.separator + fileNamePath;
    }

    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static
    class DruidStatView implements Serializable {
        private String username;
        private String password;
    }

    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static
    class SecretUrl implements Serializable {
        public static final String LOGIN_ACTION = "/secretUrl.login";
        /**
         * 拦截Uri列表
         */
        @Builder.Default
        private String[] uris = new String[]{
                "/actuator",
                "/actuator/*",
                "/actuator/*/*",
                "/swagger-ui.html",
                "/doc.html",
                "/webjars/*",
                "/swagger-resources",
                "/swagger-resources/*",
                "/swagger-resources/*/*",
                "/error",
                "/error/*",
                "/v2/*"
        };
        /**
         * 是否启用
         */
        @Builder.Default
        private boolean enable = false;
        /**
         * 用户名
         */
        @Builder.Default
        private String username = "nervenets";
        /**
         * 密码
         */
        @Builder.Default
        private String password = "!nervenets!";
    }

    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static
    class CorsConfig implements Serializable {
        @Builder.Default
        private String[] allowedOrigins = new String[]{"*"};
        @Builder.Default
        private String[] allowedHeaders = new String[]{"*"};
        private String[] exposedHeaders;
        @Builder.Default
        private String[] allowedMethods = new String[]{"*"};
        @Builder.Default
        private boolean allowCredentials = true;
    }
}
